<?php

namespace App\Entity;

use App\Enum\LogTypeEnum;
use App\Repository\PageTagRepository;
use App\Utils\Helper\GetterHelper;
use App\Utils\Helper\LogHelper;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass=PageTagRepository::class)
 * @ORM\HasLifecycleCallbacks()
 */
class PageTag {
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $value;

    /**
     * @ORM\Column(type="integer", nullable=true)
     */
    private $admin_user_id;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $created_at;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $updated_at;

    public function getId (): ?int {
        return $this->id;
    }

    public function getValue (): ?string {
        return $this->value;
    }

    public function setValue (string $value): self {
        $this->value = $value;

        return $this;
    }

    public function getCreatedAt (): ?\DateTimeInterface {
        return $this->created_at;
    }

    public function setCreatedAt (?\DateTimeInterface $created_at): self {
        $this->created_at = $created_at;

        return $this;
    }

    public function getUpdatedAt (): ?\DateTimeInterface {
        return $this->updated_at;
    }

    public function setUpdatedAt (?\DateTimeInterface $updated_at): self {
        $this->updated_at = $updated_at;

        return $this;
    }

    public function getAdminUserId (): ?int {
        return $this->admin_user_id;
    }

    public function setAdminUserId (?int $admin_user_id): self {
        $this->admin_user_id = $admin_user_id;

        return $this;
    }

    /**
     * 新建之后 hook
     * @ORM\PostPersist()
     */
    public function PostPersistHook () {
        // 日志
        $this->buildLogByCreate();
        // 缓存
        GetterHelper::getEntityManager()->getRepository(self::class)->updateCache();
    }

    /**
     * 更新之后 hook
     * @ORM\PostUpdate()
     */
    public function PostUpdateHook () {
        // 日志
        $this->buildLogByUpdate();
        // 缓存
        GetterHelper::getEntityManager()->getRepository(self::class)->updateCache();
    }

    /**
     * 删除之后 hook
     * @ORM\PostRemove()
     */
    public function PostRemoveHook () {
        // 缓存
        GetterHelper::getEntityManager()->getRepository(self::class)->updateCache();
    }

    public function buildLogByCreate () {
        $description = '标签:['.$this->getValue().']';
        LogHelper::buildLog(LogTypeEnum::TYPE_PAGE_TAG_CREATE, $description);
    }

    public function buildLogByUpdate () {
        $description = '标签:['.$this->getValue().']';
        LogHelper::buildLog(LogTypeEnum::TYPE_PAGE_TAG_UPDATE, $description, $this);
    }

    /**
     * 删除之前 日志
     * @ORM\PreRemove()
     */
    public function buildLogByDelete () {
        $description = '标签:['.$this->getValue().']';
        LogHelper::buildLog(LogTypeEnum::TYPE_PAGE_TAG_DELETE, $description);
    }
}
